文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
Part II. Getting started
If you’re just getting started with Spring Boot, or ‘Spring’ in general, this is the section for you! Here we answer the basic “what?”, “how?” and “why?” questions. You’ll find a gentle introduction to Spring Boot along with installation instructions. We’ll then build our first Spring Boot application, discussing some core principles as we go.
如果你刚开始学习Spring Boot或通常所说的Spring,这部分就是为你准备的!这部分中我们回答了基本的”what?”,”how”,”why?”问题,并在安装说明中介绍Spring Boot。我们将构建第一个Spring Boot应用,讨论一些我们遵循的核心原则。
8. Introducing Spring Boot
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
Spring Boot 使创建独立的、产品级的、基于Spring的应用变得更容易,你只需要运行run
即可。我们采用不变的Spring平台和第三方库,因此你可以几乎无差别的使用Spring Boot。大多数Spring Boot应用只需要很少的Spring配置。
You can use Spring Boot to create Java applications that can be started using java-jar
or more traditional war deployments. We also provide a command line tool that runs “spring scripts”.
你可以使用Spring Boot创建Java应用,用java-jar
或更传统的war包来部署应用。我们也提供了运行”Spring脚本”的命令行工具。
Our primary goals are:
Provide a radically faster and accessible getting started experience for all Spring development.
Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.
Provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration).
Absolutely no code generation and no requirement for XML configuration.
我们的主要目标是:
为所有Spring开发者提供一个从根本上更迅速可用的入门经验。
坚持开箱即用,当默认设置不满足需求时可以快速避免默认设置。
为具有许多类的工程提供一系列常用的非功能特性(例如嵌入式服务器、安全、度量、健康检查、外部配置)。
绝对没有XML配置代码产生和XML配置需求。
9. System Requirements
默认情况下,Spring Boot 2.0.0.BUILD-SNAPSHOT需要Java 7和Spring Framework 5.0.0.BUILD-SNAPSHOT及以上。你可以通过一些额外配置在Java 6下使用Spring Boot。更多细节请看80.11小节,”怎样使用Java 6”。明确的构建支持有Maven (3.2+)和Gradle (1.12 or 2.x),不支持Gradle 3。
虽然你可以在Java 6或Java 7下使用Spring Boot,但我们建议尽可能的使用Java 8。
9.1 Servlet containers
The following embedded servlet containers are supported out of the box:
Name | Servlet Version | Java Version |
---|---|---|
Tomcat 8 | 3.1 | Java 7+ |
Tomcat 7 | 3.0 | Java 6+ |
Jetty 9.3 | 3.1 | Java 8+ |
Jetty 9.2 | 3.1 | Java 7+ |
Jetty 8 | 3.0 | Java 6+ |
Undertow 1.3 | 3.1 | Java 7+ |
You can also deploy Spring Boot applications to any Servlet 3.0+ compatible container.
下面的嵌入式servlet
容器支持开箱即用:
Name | Servlet Version | Java Version |
---|---|---|
Tomcat 8 | 3.1 | Java 7+ |
Tomcat 7 | 3.0 | Java 6+ |
Jetty 9.3 | 3.1 | Java 8+ |
Jetty 9.2 | 3.1 | Java 7+ |
Jetty 8 | 3.0 | Java 6+ |
Undertow 1.3 | 3.1 | Java 7+ |
你也可以部署Spring Boot应用到任何兼容Servlet 3.0+的容器。
10. Installing Spring Boot
Spring Boot can be used with “classic” Java development tools or installed as a command line tool. Regardless, you will need Java SDK v1.6 or higher. You should check your current Java installation before you begin:
Spring Boot可以和”经典”的Java开发工具一起使用,也可作为一个命令行工具来进行安装。不管你是需要Java SDK v.16还是更高版本,在开始之前你应该检查你当前安装的Java版本。
1 | $ java -version |
If you are new to Java development, or if you just want to experiment with Spring Boot you might want to try the Spring Boot CLI first, otherwise, read on for “classic” installation instructions.
如果你是Java开发的新手,或你只想尝试Spring Boot,你可能首先想试一下Spring Boot CLI,否则,请读”经典”的安装说明。
Although Spring Boot is compatible with Java 1.6, if possible, you should consider using the latest version of Java.
虽然Spring Boot兼容Java 1.6,但可能的话,你应该考虑使用Java的最新版本。
10.1 Installation instructions for the Java developer
You can use Spring Boot in the same way as any standard Java library. Simply include the appropriate spring-boot-*.jar
files on your classpath. Spring Boot does not require any special tools integration, so you can use any IDE or text editor; and there is nothing special about a Spring Boot application, so you can run and debug as you would any other Java program.
你可以使用Spring Boot像使用任何标准Java库一样。简单的在你的classpath
中包含恰当spring-boot-*.jar
即可。Spring Boot不需要任何特定的工具集成,因此你可以使用任何IDE或文本编辑器;Spring Boot应用没有什么特别的,你可以像其它的Java程序一样来运行和调试。
Although you could just copy Spring Boot jars, we generally recommend that you use a build tool that supports dependency management (such as Maven or Gradle).
尽管你可以只拷贝Spring Boot的jars,但通常我们建议你使用一个支持依赖管理的构建工具(例如Maven或Gradle)。
10.1.1 Maven installation
Spring Boot is compatible with Apache Maven 3.2 or above. If you don’t already have Maven installed you can follow the instructions at maven.apache.org.
Spring Boot兼容Apache Maven 3.2或以上。如果你没有安装Maven,你可以根据maven.apache.org的说明来安装。
On many operating systems Maven can be installed via a package manager. If you’re an OSX Homebrew user try
brew install maven
. Ubuntu users can runsudo apt-get install maven
.
在许多操作系统上Maven都能通过包管理器来安装。如果你是一个OSX Homebrew用户可以通过
brew install maven
安装。Ubuntu用户可以通过sudo apt-get install maven
安装。
Spring Boot dependencies use the org.springframework.boot
groupId
. Typically your Maven POM file will inherit from the spring-boot-starter-parent
project and declare dependencies to one or more “Starters”. Spring Boot also provides an optional Maven plugin to create executable jars.
Spring Boot依赖使用org.springframework.boot
groupId
。通常你的Maven POM文件将继承spring-boot-starter-parent
工程并声明一个或多个“Starters”依赖。Spring Boot也提供一个可选的Maven插件来创建可执行的jar包。
Here is a typical pom.xml
file:
下面是一个典型的pom.xml
文件:
1 | <?xml version="1.0" encoding="UTF-8"?> |
The
spring-boot-starter-parent
is a great way to use Spring Boot, but it might not be suitable all of the time. Sometimes you may need to inherit from a different parent POM, or you might just not like our default settings. See Section 13.2.2, “Using Spring Boot without the parent POM” for an alternative solution that uses animport
scope.
spring-boot-starter-parent
是使用Spring Boot的一种极好的方式,但它可能不是一直都合适的。有时你可能需要继承一个不同的父POM,或者你可能不喜欢我们的默认设置。请看13.2.2小节,『没有父POM的情况下使用Spring Boot』是一种可替代的解决方案,它使用了import
作用域。
10.1.2 Gradle installation
Spring Boot is compatible with Gradle 1.12 or 2.x. 2.14.1 is recommended. Gradle 3 is not supported. If you don’t already have Gradle installed you can follow the instructions at www.gradle.org/.
Spring Boot兼容Gradle 1.12或2.x。推荐使用2.14.1。不支持Gradle 3。如果你没有安装Gradle,你可以根据www.gradle.org/的介绍来安装。
Spring Boot dependencies can be declared using the org.springframework.boot group
. Typically your project will declare dependencies to one or more “Starters”. Spring Boot provides a useful Gradle plugin that can be used to simplify dependency declarations and to create executable jars.
Spring Boot依赖使用org.springframework.boot
groupId
。通常你的工程会声明一个或多个“Starters”依赖。Spring Boot提供了一个有用的Gradle插件用来简化依赖声明并创建可执行的jar包。
Gradle Wrapper
The Gradle Wrapper provides a nice way of “obtaining” Gradle when you need to build a project. It’s a small script and library that you commit alongside your code to bootstrap the build process. See https://docs.gradle.org/2.14.1/userguide/gradle_wrapper.html for details.
Gradle Wrapper
当你需要构建一个工程时,Gradle Wrapper提供了一种很好的方式来『获得』Gradle。它是一小段脚本和库,并随着你的代码一起提交来引导构建过程。更多细节请看https://docs.gradle.org/2.14.1/userguide/gradle_wrapper.html。
Here is a typical build.gradle
file:
下面是一个典型的build.gradle
文件:
1 | buildscript { |
10.2 Installing the Spring Boot CLI
The Spring Boot CLI is a command line tool that can be used if you want to quickly prototype with Spring. It allows you to run Groovy scripts, which means that you have a familiar Java-like syntax, without so much boilerplate code.
Spring Boot CLI是一个命令行工具,可以用来快速搭建基于Spring的原型。它允许你运行Groovy脚本,这意味着你有一个熟悉的类Java语法,没有很多的样本代码。
You don’t need to use the CLI to work with Spring Boot but it’s definitely the quickest way to get a Spring application off the ground.
你不需要使用CLI来使用Spring Boot但它是开始得到一个基于Spring应用的最快方式。
10.2.1 Manual installation
You can download the Spring CLI distribution from the Spring software repository:
你可以从Spring软件仓库下载Spring CLI发行版:
Cutting edge snapshot distributions are also available.
最新的snapshot distributions也是可获得的。
Once downloaded, follow the INSTALL.txt instructions from the unpacked archive. In summary: there is a spring
script (spring.bat
for Windows) in a bin/
directory in the .zip
file, or alternatively you can use java -jar
with the .jar
file (the script helps you to be sure that the classpath is set correctly).
一旦下载完成,按照存档INSTALL.txt中的 介绍来安装。总结:bin/
目录的.zip
文件中有一个spring
脚本(Windows中是spring.bat
),或者你可以使用java -jar
运行.jar
文件(这个脚本帮你确保正确设置classpath)。
10.2.2 Installation with SDKMAN!
SDKMAN! (The Software Development Kit Manager) can be used for managing multiple versions of various binary SDKs, including Groovy and the Spring Boot CLI. Get SDKMAN! from sdkman.io and install Spring Boot with:
SDKMAN! (The Software Development Kit Manager)能用来管理各种二进制SDKs的多个版本,包括Groovy和Spring Boot CLI。从sdkman.io中得到SDKMAN!并安装Spring Boot:
1 | $ sdk install springboot |
If you are developing features for the CLI and want easy access to the version you just built, follow these extra instructions.
如果你在为CLI开发功能并向很容易的访问你够简单版本,按照这些额外的介绍去做。
1 | $ sdk install springboot dev /path/to/spring-boot/spring-boot-cli/target/spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin/spring-2.0.0.BUILD-SNAPSHOT/ |
This will install a local instance of spring
called the dev
instance. It points at your target build location, so every time you rebuild Spring Boot, spring
will be up-to-date.
这将会安装一个名为dev
的本地spring
实例。它指向你的目标构建位置,因此你每次重新构建Spring Boot,spring
将是最新的。
You can see it by doing this:
按下面去做你会看到:
1 | $ sdk ls springboot |
10.2.3 OSX Homebrew installation
If you are on a Mac and using Homebrew, all you need to do to install the Spring Boot CLI is:
如果你在Mac上使用Homebrew,安装Spring Boot CLI所有你需要做的是:
1 | $ brew tap pivotal/tap |
Homebrew will install spring
to /usr/local/bin
.
Homebrew会在/usr/local/bin
目录安装spring
。
If you don’t see the formula, your installation of brew might be out-of-date. Just execute
brew update
and try again.
如果你没有看到公式,你安装的brew可能过时了。执行
brew update
并重新再试一次。
10.2.4 MacPorts installation
If you are on a Mac and using MacPorts, all you need to do to install the Spring Boot CLI is:
如果你在Mac上使用MacPorts,安装Spring Boot CLI所有你需要做的是:
1 | $ sudo port install spring-boot-cli |
10.2.5 Command-line completion
Spring Boot CLI ships with scripts that provide command completion for BASH and zsh shells. You can source
the script (also named spring
) in any shell, or put it in your personal or system-wide bash completion initialization. On a Debian system the system-wide scripts are in /shell-completion/bash
and all scripts in that directory are executed when a new shell starts. To run the script manually, e.g. if you have installed using SDKMAN!
Spring Boot CLI附带的脚本为BASH和zsh shells提供了命令行实现。你可以在任何shell中source
脚本(也称为spring
),或将它放在用户或系统范围内的bash完成初始化。在Debian系统中系统范围内的脚本是在/shell-completion/bash
目录中,当新shell启动时,目录下的所有脚本都被执行。如果你装了SDKMAN!,为了手动运行脚本,执行下面的命令:
1 | $ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring |
If you install Spring Boot CLI using Homebrew or MacPorts, the command-line completion scripts are automatically registered with your shell.
如果你想使用Homebrew或MacPorts安装Spring Boot CLI,命令行完成脚本会自动在shell中注册。
10.2.6 Quick start Spring CLI example
Here’s a really simple web application that you can use to test your installation. Create a file called app.groovy
:
这儿有一个真实的简单的web应用,你可以用来测试你的安装。创建一个文件叫app.groovy
:
1 | @RestController |
Then simply run it from a shell:
然后简单的从shell中运行它:
1 | $ spring run app.groovy |
It will take some time when you first run the application as dependencies are downloaded. Subsequent runs will be much quicker.
当你第一次运行应用时它需要一段时间,因为要下载依赖。接下来运行会更快。
Open localhost:8080 in your favorite web browser and you should see the following output:
在你最喜欢的浏览器中输入localhost:8080,你应该会看到下面的结果:
1 | Hello World! |
10.3 Upgrading from an earlier version of Spring Boot
If you are upgrading from an earlier release of Spring Boot check the “release notes” hosted on the project wiki. You’ll find upgrade instructions along with a list of “new and noteworthy” features for each release.
如果正在升级你的Spring Boot,检查project wiki中的『发布说明』。你将找到升级说明,说明中有每一次发布的特性列表。
To upgrade an existing CLI installation use the appropriate package manager command (for example brew upgrade
) or, if you manually installed the CLI, follow the standard instructions remembering to update your PATH environment variable to remove any older references.
为了升级现有的CLI,要使用合适的包管理命令(例如brew upgrade
),如果你手动安装的CLI,要按照标准的说明去做,为了移除旧的引用要记住更新你的PATH环境变量。